package com.chinasofti.framework.dataaccess;

import java.util.ArrayList;
import java.util.HashMap;

import com.chinasofti.framework.data.Entity;
import com.chinasofti.framework.data.FieldType;

public class Filter extends Entity{

	public static enum Relation {
		AND,
		OR
	}

	private static HashMap<String, FieldType> m_fields;
	private static final String ENTITY_NAME = "Filter";
	private static final String KEY_FIELD = "";
	
	public static final String FIELD_CHILDFILTERS = "ChildFilters";
	public static final String FIELD_CONDITION = "Condition";
	public static final String FIELD_RELATION = "Relation";
	
	@Override
	protected void init() {
		if(m_fields == null) {
			m_fields = new HashMap<String, FieldType>();
			m_fields.put(FIELD_CHILDFILTERS, new FieldType(Filter.class, false));
			m_fields.put(FIELD_CONDITION, new FieldType(Condition.class, false));
			m_fields.put(FIELD_RELATION, new FieldType(Relation.class));
		}
	}
	
	@Override
	public FilterParser getParser() {
		return new FilterParser();
	}

	@Override
	public HashMap<String, FieldType> getFieldTypes() {
		return m_fields;
	}

	@Override
	public String getKeyField() {
		return KEY_FIELD;
	}

	@Override
	public String getEntityName() {
		return ENTITY_NAME;
	}
	
	//鑷畾涔変唬鐮�
	public static  Filter create(Relation relation) {
		Filter filter = new Filter();
		filter.set("Relation", relation);
		return filter;
	}
	
	public void addCondition(Condition condition) {
		@SuppressWarnings("unchecked")
		ArrayList<Condition> conditions = (ArrayList<Condition>)get("Condition");
		if(conditions == null) {
			conditions = new ArrayList<Condition>();
			set("Condition", conditions);
		}
		conditions.add(condition);
	}
	
	public void addFilter(Filter filter) {
		ArrayList<Filter> filters = null;
		if(get("Filter") == null) {
			filters = new ArrayList<Filter>();
			set("Filter", filters);
		}
		filters.add(filter);
	}
	
	@SuppressWarnings("unchecked")
	public ArrayList<Filter> getChildFilters() {
		return (ArrayList<Filter>)get("Filter");
	}
	
	@SuppressWarnings({ "unchecked" })
	public ArrayList<Condition> getConditions() {
		return (ArrayList<Condition>)get("Condition");
	}
	
	public void ClearConditions() {
		ArrayList<Condition> conditions = getConditions();
		if(conditions != null) {
			conditions.clear();
		}
	}
	
	public void ClearChildFilters() {
		ArrayList<Filter> filters = getChildFilters();
		if(filters != null) {
			getChildFilters().clear();
		}
	}
	
	public Relation getRelation() {
		return (Relation)get("Relation");
	}
	
	public String toSqlString() {
		String str = "";
		ArrayList<Filter> filters = getChildFilters();
		ArrayList<Condition> conditions = getConditions();
		Relation relation = getRelation();
		
		for(int i = 0; filters != null && i < filters.size(); i++) {
			Filter filter = (Filter)filters.get(i);
			if(str.equals("")) {
				str += "(" + filter.toSqlString() + ")";
			}
			else { 
				str += " " + relation.toString() + " (" + filter.toSqlString() + ")"; 
			}
		}
		
		for(int i = 0; conditions != null && i < conditions.size(); i++) {
			Condition condition = conditions.get(i);
			if(str.equals("")) {
				str += condition.toSqlString();
			}
			else {
				str += " " + relation.toString() + " " + condition.toSqlString();
			}
		}
		return str;
	}
}
